package com.qczl.pme.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.qczl.core.controller.BaseController;

/**
 * 二维码生成
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/qrcode")
public class QRCodeController extends BaseController {
	Logger log = Logger.getLogger(this.getClass());
	/**
	 * 驻村干部签到二维码生成
	 * @param request
	 * @param response
	 * @param w
	 * @param text
	 * @throws IOException
	 * @throws WriterException
	 */
	@RequestMapping(value="/v1",method={RequestMethod.GET,RequestMethod.POST})
	public void imageV1(HttpServletRequest request, HttpServletResponse response,Integer w,String text){
		if(null==w||0==w){
			w=400;
		}
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, w, w, hints);
			response.setContentType("image/png");
			OutputStream os = response.getOutputStream();
			ImageIO.write(MatrixToImageWriter.toBufferedImage(bitMatrix), "png", os);
		} catch (WriterException e) {
			log.error("接口异常！",e);
		} catch (IOException e) {
			log.error("接口异常！",e);
		}
		
	}

}
